﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using Prism.Commands;
using Prism.Events;
using Unity;
using Prism.Unity;
using Prism.Ioc;
using MS.Common;
using wpf.Triage.DataModel;
using Prism.Regions;
using MS.Common.Helpers;
using Wpf.Models;
using MS.Common.Theme;
using System.Windows.Controls;
using System.Collections.ObjectModel;
using Newtonsoft.Json;
using MS.Common.Event;
using wpf.Triage.Models;
using ZdfFlatUI;
using wpf.Triage.Views;

namespace wpf.Triage.ViewModels
{

    public class TriageResgisterViewModel : VmBase, INavigationAware
    {
        #region 模板

        private readonly Notifiaction notifiaction;
        private readonly ThemeHelper themeHelper = new ThemeHelper();

        private DataTemplate fullDataTemplate;
        /// <summary>
        /// 界面主题
        /// </summary>
        public DataTemplate FullDataTemplate
        {
            get => fullDataTemplate;
            set
            {
                fullDataTemplate = value;
                RaisePropertyChanged(nameof(FullDataTemplate));
            }
        }
        #endregion

        #region 事件
        /// <summary>
        ///  清空事件   2022-01-20
        /// </summary>
        public DelegateCommand<object> ClearCommand { get; private set; }
        /// <summary>
        ///  设置默认值事件   2022-01-20
        /// </summary>
        public DelegateCommand<object> SetDefalutTextCommand { get; private set; }
        public DelegateCommand<object> ValidateCommand { get; private set; }
        public DelegateCommand<object> PatListCommand { get; private set; }

        /// <summary>
        ///  确认分诊事件   2022-01-20
        /// </summary>
        public DelegateCommand<object> SaveCommand { get; private set; }
        /// <summary>
        ///  删除事件   2022-01-20
        /// </summary>
        public DelegateCommand<object> DeleteCommand { get; private set; }
        /// <summary>
        ///  保存草稿事件   2022-01-20
        /// </summary>
        public DelegateCommand<object> CacheCommand { get; private set; }
        /// <summary>
        /// <summary>
        ///  改变主题事件  
        /// </summary>
        public DelegateCommand<object> ChangeThemeCommand { get; private set; }
        #endregion

        #region 属性
        private Tr_PatientVisitInfo _PatVisInfo;
        /// <summary>
        /// 基本信息
        /// </summary>
        public Tr_PatientVisitInfo PatVisInfo
        {
            get { return _PatVisInfo; }
            set
            {
                _PatVisInfo = value;
                RaisePropertyChanged(nameof(PatVisInfo));
            }
        }

        private Tr_VitalsignRecordInfo _VitRecInfo;
        /// <summary>
        /// 生命体征信息
        /// </summary>
        public Tr_VitalsignRecordInfo VitRecInfo
        {
            get { return _VitRecInfo; }
            set
            {
                _VitRecInfo = value;

                RaisePropertyChanged(nameof(VitRecInfo));
            }
        }
        private VitInfoCheck _VitCheckInfo;
        public UserControl Uc;

        /// <summary>
        /// 验证生命体征数据
        /// </summary>
        public VitInfoCheck VitCheckInfo
        {
            get { return _VitCheckInfo; }
            set
            {
                _VitCheckInfo = value;
                RaisePropertyChanged(nameof(VitCheckInfo));
            }
        }
        #endregion

        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="container"></param>
        /// <param name="eventAggregator"></param>
        /// <param name="regionManager"></param>
        public TriageResgisterViewModel(IContainerExtension container, IEventAggregator eventAggregator, IRegionManager regionManager) : base(container, eventAggregator, regionManager)
        {
            notifiaction = container.Resolve<Notifiaction>();
            PatListCommand = new DelegateCommand<object>(OnPatListCommand);
            ChangeThemeCommand = new DelegateCommand<object>(OnChangeTheme);
            ValidateCommand = new DelegateCommand<object>(OnValidateCommand);
            this.SaveCommand = new DelegateCommand<object>(OnSaveCommand);
            this.CacheCommand = new DelegateCommand<object>(OnCacheCommand);
            this.DeleteCommand = new DelegateCommand<object>(OnDeleteCommand);
            this.SetDefalutTextCommand = new DelegateCommand<object>(OnSetDefalutText);
            this.ClearCommand = new DelegateCommand<object>(ReSetShowData);
            PatVisInfo = new Tr_PatientVisitInfo() { VISITDATE = DateTime.Now };
            VitRecInfo = new Tr_VitalsignRecordInfo();
            VitCheckInfo = new VitInfoCheck();
        }

        /// <summary>
        /// 保存草稿操作   2022-01-20
        /// </summary>
        public void OnCacheCommand(object obj)
        {
            ResponseApi<CommonResult> Res = null;
            try
            {
                ZMessageBox.Show(Window.GetWindow(Uc), "您目前使用的浏览器版本过低，可能导致产品部分功能无法正常使用", "", MessageBoxButton.YesNoCancel, EnumPromptType.Warn);

            }
            catch (Exception ex)
            {
                LogHelper.GetLogHelper().Error("分诊保存草稿" + ex);
            }
        }
        /// <summary>
        /// 分诊保存操作   2022-01-20
        /// </summary>
        public void OnSaveCommand(object obj)
        {
            try
            {
                UserControl content = container.Resolve<PatientList>();
                ModalHelper.Show(Window.GetWindow(Uc), content, "患者列表", 400, 400);
            }
            catch (Exception ex)
            {
                LogHelper.GetLogHelper().Error("分诊保存时出现异常：" + ex);
            }
        }
        /// <summary>
        /// 分诊删除操作   2022-01-20
        /// </summary>
        public void OnDeleteCommand(object obj)
        {
            UserControl content = container.Resolve<PatientList>();
            ModalHelper.ShowMDiag(Window.GetWindow(Uc), content, "患者列表", 400, 400);
        }
        /// <summary>
        ///生命体征点击设置默认值   2022-01-20
        /// </summary>
        public void OnSetDefalutText(object obj)
        {
            if (obj != null)
            {
                List<string> proNames = obj.ToString()?.Split('|')?.ToList();
                if (proNames != null && proNames.Count >= 1)
                {
                    PropertyHelper.FindProSetValue(proNames[0], proNames[1], VitCheckInfo);
                }
            }
        }
        /// <summary>
        ///清空按钮触发   2022-01-20
        /// </summary>
        public void ReSetShowData(object obj)
        {
            PatVisInfo = new Tr_PatientVisitInfo() { VISITDATE = DateTime.Now, HAPPENDATE = DateTime.Now };
        }
        private void OnPatListCommand(object obj)
        {


        }
        /// <summary>
        /// 点击验证
        /// </summary>
        /// <param name=""></param>
        private void OnValidateCommand(object obj)
        {
            string proName = obj == null ? "" : obj.ToString();
            if (proName == string.Empty)
                return;
            string setProName = proName + "_MSG";
            string text = PropertyHelper.FindProValue<string>(proName, VitRecInfo);
            string value = null;
            if (text == "123")
            {
                value = "出现错误";
            }
            else
            {
                value = null;
            }
            PropertyHelper.FindProSetValue(setProName, value, VitCheckInfo);
        }

        /// <summary>
        /// 切换主题方法
        /// </summary>
        private void OnChangeTheme(object obj)
        {
            var key = obj != null ? obj.ToString() : string.Empty;
            if (string.IsNullOrEmpty(key))
                key = "1";
            FullDataTemplate = themeHelper.GetDataTemplate("FullDataTemplate" + key);
            notifiaction.AddNotifiaction(new NotifiactionModel() { Title = "成功", Content = "切换成功！", NotifiactionType = EnumPromptType.Success });
            eventAggregator.GetEvent<MessageBoxEvent>().Publish("");
        }
        public bool IsNavigationTarget(NavigationContext navigationContext)
        {
            return true;
        }

        public void OnNavigatedFrom(NavigationContext navigationContext)
        {

        }
        public void OnNavigatedTo(NavigationContext navigationContext)
        {
            string Type = navigationContext.Parameters["Type"]?.ToString();
            if (string.IsNullOrEmpty(Type))
                Type = "1";
            OnChangeTheme(Type);
        }
    }
}
